home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Talks & Papers / Timothy Knox / yerk 3.66 / Asm source / TopLevel < prev    next >
Text File  |  1994-06-24  |  5KB  |  191 lines

  1. \ Assembler            ReeseWarner            3/85
  2. \  02/28/86  GDC  Fixed :MCODE
  3. \   7/10/86  mrh  Fixed ASM to accept a label on a blank line
  4. \   6/06/93  rfl  added documentation fix...since double pass assm, need to
  5. \                  divide line# by 2.
  6.  
  7. 0 -> dlevel
  8.  
  9. 0 value endLen
  10. 0 value endAddr
  11.  
  12. : MOVEBACK
  13.     addr: topFile 3 charCount negate (lseek) drop
  14. ;
  15.  
  16. : INITP     { #pass -- }
  17.     #pass -> pass
  18.     0 -> charCount
  19.     tiblen -> pos
  20.     0 -> linect
  21.     0 -> codePos
  22.     0 -> errflag
  23.     0 -> storedToken
  24. ;
  25.  
  26. \ Eliminates any blank lines
  27. : KILLEOLS  ( -- )
  28.     BEGIN
  29.         nextToken 4 = not
  30.     UNTIL
  31. ;
  32.  
  33. : DOMIDDLE
  34.     msg" top"
  35.     errflag
  36.     IF                                     \ if error then 
  37.         211 asmError                       \    abort
  38.         abort
  39.     ELSE                                   \ else
  40.         moveBack                           \    set up pass 2
  41.         2 initP
  42.     THEN
  43. ;
  44.  
  45. \ Assemble instructions, main control loop 
  46. : ASM   { \ mnemonic endFlag -- }
  47.     false -> endFlag
  48.     BEGIN
  49.         endFlag
  50.         IF                                 \ if end flag then
  51.             false                          \    get out of loop
  52.         ELSE                               \ else
  53.             KillEols                       \    get rid of blank lines
  54.             start: token                   \    get a token
  55.             EndAddr EndLen get: token s= not val" ENDflag F=Found"
  56.         THEN                               \    if end of asm code then exit
  57.     WHILE
  58.         token query: codes -> mnemonic     \ get label of opcode
  59.         mnemonic val" mnemonic is" 0=      \ check for label
  60.         IF                                 \ Label
  61.             msg" It's a label!"
  62.             pass 1 =
  63.             IF                                       \ if pass 1 then
  64.                 codePos 2* token enter: symtab       \ put label on simtab
  65.             THEN
  66.         ELSE
  67.             opsize: mnemonic -> opSize
  68.             mnemonic val" final mnemonic is" 0= 
  69.             IF
  70.                 true -> endFlag                      \ end of asm code
  71.             ELSE
  72.                 pass 1 =
  73.                 IF
  74.                     length: mnemonic \ dup . cr      \ for debuging
  75.                     ++> codePos                      \ add length to codepos
  76.                 ELSE
  77.                     here -> keephere                 \ builds bit codes
  78.                     build: mnemonic
  79.                     here keephere - ++> codePos
  80.                 THEN
  81.             THEN
  82.         THEN
  83.     REPEAT
  84.     pass 1 =
  85.     IF
  86.         doMiddle
  87.         ASM msg" after pass-2"                   \ starts pass 2 recursively
  88.     ELSE
  89.         msg" pre-next"
  90.         [compile] next,
  91.         msg" post-next"
  92.     THEN
  93. ;
  94.  
  95. TRUE value InitASM?     \ jaf 12/13 has ASM been init'd?
  96.  
  97. 2 0 0 to: next.state
  98. 1 0 1 to: next.state
  99. 0 0 2 to: next.state
  100. 0 0 3 to: next.state
  101. 0 1 0 to: next.state
  102. 1 1 1 to: next.state
  103. 0 1 2 to: next.state
  104. 0 1 3 to: next.state
  105. 2 2 0 to: next.state
  106. 2 2 1 to: next.state
  107. 0 2 2 to: next.state
  108. 0 2 3 to: next.state
  109.  
  110. 0 0 0 to: action
  111. 0 0 1 to: action
  112. 2 0 2 to: action
  113. 4 0 3 to: action
  114. 1 1 0 to: action
  115. 0 1 1 to: action
  116. 1 1 2 to: action
  117. 1 1 3 to: action
  118. 0 2 0 to: action
  119. 0 2 1 to: action
  120. 3 2 2 to: action
  121. 3 2 3 to: action
  122.  
  123. : InitASM { \ xecho -- }     \ jaf 12/13 created asm init word
  124.  
  125. new: token
  126. decho -> xecho -echo    \ Preserve echo status
  127. " Operands" loadOps                   \ get operands in "operands"
  128. " AsmCodes" loadcodes                 \ get opcode codes in AsmCodes
  129. false -> InitASM?
  130. xecho -> decho          \ Restore echo status
  131. ;
  132.  
  133. : EndASM    \ jaf 12/13 use when done with ASM
  134.     dispose: codes
  135.     dispose: operands
  136.     dispose: symTab
  137.     release: token
  138.     TRUE -> INITASM?
  139. ;
  140.  
  141. 0 value keepLine
  142. \ :CODE - word exported to dictionary. Begins compilation of code word
  143. : :CODE
  144.     " ;CODE" -> endLen -> endAddr
  145.     dispose: symTab
  146.     create                      \ start the :code definition
  147.     line# -> keepLine
  148.     0 -> line#
  149.     InitASM? IF InitASM THEN    \ jaf 12/13 init ASM if not already done
  150.     1 initP
  151.     asm                         \ does the work
  152.     msg" post-asm"
  153.     line# 2/ keepLine + -> line#
  154.     query: topfile drop
  155. ;
  156.  
  157. \ ADD-HDR-CODE - this word ,'s in the appropriate machine instructions to 
  158. \  set up a code method definition. Whatever they might be.
  159. : ADD-HDR-CODE
  160.     here 24 + ,     \ 0CFA - for public objects
  161.     here 4+ ,       \ 1CFA - for ivars
  162.     $ 301C w,       \ move.w (a4)+,d0
  163.     $ 6C02 w,       \ bge +2
  164.     $ 4280 w,       \ clr.l d0
  165.     $ 2445 w,       \ movea.l d5,a2
  166.     $ 2452 w,       \ movea.l (a2),a2
  167.     $ D5C0 w,       \ adda.l d0,a2
  168.     $ 4EFA0004 ,    \ JMP to code
  169.     $ 245F w,       \ movea.l (a7)+,a2
  170. ;
  171.  
  172. \ :MCODE - word exported to dictionary. Begins compilation of code method
  173. : :MCODE    { \ selID -- } ?class
  174.     " ;MCODE" -> endLen -> endAddr
  175.     dispose: symTab
  176.     getSelect -> selID
  177.     selID ^class mfa ((findm))      \ is method already defined
  178.     IF
  179.        here count type ." method redefined as code" cr drop \ drop method addr
  180.     THEN
  181.     here selID w,               \ name is selector's hashed value
  182.     ^class mfa dup @            \ get mfa, old link
  183.     , !                         \ establish the links
  184.     add-hdr-code
  185.     InitASM? IF InitASM THEN    \ jaf 12/13 init ASM if not already done
  186.     1 initP
  187.     asm
  188.     query: topfile drop
  189. ;
  190. immediate
  191.